package eg.com.itiga.business.company;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.List;
import java.util.Set;

import eg.com.itiga.business.base.BaseServiceImpl;
import eg.com.itiga.business.exception.BusinessException;
import eg.com.itiga.business.utils.Mail;
import eg.com.itiga.domain.ContactDetails;
import eg.com.itiga.domain.RegCompany;
import eg.com.itiga.domain.Role;
import eg.com.itiga.domain.User;
import eg.com.itiga.repository.base.BaseDao;
import eg.com.itiga.repository.company.RegCompanyDao;
import eg.com.itiga.repository.factory.RepositoryFactory;
import eg.com.itiga.repository.role.RoleDao;
import eg.com.itiga.repository.user.UserDao;

public class RegCompanyServiceImpl extends BaseServiceImpl implements RegCompanyService{

	/**
	 * @Author Bola
	 * Approve already registered company
	 * @param compID
	 * @throws BusinessException
	 */
	public void approveCompany(int compID) throws BusinessException 
	{
		System.out.println("in business....");
		RegCompanyDao regCompanyDao = null;
		UserDao userDao=null;
		User newUser=null;
		Role userRole=null;
		RoleDao roleDao=null;
		BaseDao baseDao=null;
		String password=null;
		String userName=null;
		String companyMail=null;
		Set<ContactDetails> companyDetails=null;
		try
		{
			regCompanyDao = RepositoryFactory.createRegCompanyDao();
			userDao=RepositoryFactory.createUserDao();
			roleDao=RepositoryFactory.createRoleDao();
			newUser=new User();			
			
			regCompanyDao.beginTransaction();
			
			RegCompany regCompany = (RegCompany) regCompanyDao.find(RegCompany.class, compID);
			userRole=(Role)roleDao.find(Role.class, Role.COMPANYROLE);
			regCompany.setCState(RegCompany.APPROVED);
			regCompany.setRegIsApproved(true);
			companyDetails=regCompany.getContactDetailses();						
			companyMail=companyDetails.iterator().next().getConEmail();
			
			//generating the User Info and sending the mail
			//companyMail=regCompany.getEmail();
			userName=regCompany.getCName();
			password=generatePassword();
			
			System.out.println("Pass :"+password);
			System.out.println("UserName :"+ userName);
			
			newUser.setUserName(userName);
			newUser.setUserPassword(password);
			newUser.setRole(userRole);			
			userDao.saveOrUpdate(newUser);
			
			regCompany.setUser(newUser);
			//regCompanyDao.saveOrUpdate(regCompany);
			
			Mail mailObj=new Mail();			
			mailObj.setReciever(companyMail);
			//mailObj.setSenderEmail("iti.application@gmail.com");
			//mailObj.setSenderPassword("iti123456");			
			mailObj.setSubject("Mail Test");
			mailObj.replaceWord("userName", newUser.getUserName());
			mailObj.replaceWord("password", newUser.getUserPassword());			
			mailObj.send();			
			
			regCompanyDao.commit();
		}
		catch (Exception e) {
			regCompanyDao.rollback();
			e.printStackTrace();
			throw new BusinessException("Approve Company has been failed");
		}
	}

	/**
	 * @Author Bola
	 * Reject already registered company
	 * @param compID
	 * @throws BusinessException
	 */
	public void rejectCompany(int compID) throws BusinessException {

		RegCompanyDao regCompanyDao = null;
		try
		{
			regCompanyDao = RepositoryFactory.createRegCompanyDao();
			regCompanyDao.beginTransaction();
		
			RegCompany regCompany = (RegCompany) regCompanyDao.find(RegCompany.class, compID);
		
			regCompany.setRegIsApproved(false);
		
			regCompanyDao.commit();
		}
		catch (Exception e) {
			regCompanyDao.rollback();
			e.printStackTrace();
			throw new BusinessException("Approve Company has been failed");
		}
	}

	/**
	 * @Author Mostafa 
	 * get a list of Company Requests
	 * @return List<RegCompany>
	 */
	public List<RegCompany> getCompanyRequests() throws BusinessException {
		
		RegCompanyDao regCompanyDao = null;
		List<RegCompany> companiesRequest = null;
		try
		{
			regCompanyDao = RepositoryFactory.createRegCompanyDao();
			regCompanyDao.beginTransaction();
		
			companiesRequest = regCompanyDao.getCompanyRequests();
			
			regCompanyDao.commit();
		}
		catch (Exception e) {
			regCompanyDao.rollback();
			e.printStackTrace();
			throw new BusinessException("Approve Company has been failed");
		}
		
		return companiesRequest;
	}

	/**
	 * @Author Bola
	 * Register new company to the system
	 */
	public void requestRegister(RegCompany regCompany) throws BusinessException {
		RegCompanyDao regCompanyDao = null;
		try {
			regCompanyDao = RepositoryFactory.createRegCompanyDao();
			regCompanyDao.beginTransaction();			
			regCompanyDao.requestRegistration(regCompany);
		
			regCompanyDao.commit();
		} catch(Exception e) {
			regCompanyDao.rollback();
			e.printStackTrace();
			throw new BusinessException("Register company had been failed");
		}
	}
	
	/**
	 * @author Mostafa
	 * generate Random password
	 * @return genratedPassword
	 */
	private String generatePassword(){
		//int pwdLength=-1;		
		String password=null;
		SecureRandom random = new SecureRandom();        
        password = new BigInteger(50, random).toString(32);          
		return password;
		
	}	
}
